/*
 * CS_REQ_MOVE_TO_WAREHOUSE.cpp
 *
 *  Created on: 2013. 10. 18.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_MOVE_TO_WAREHOUSE(User * pUser, ::Network::Packet * pPacket)
{
/*	CS_REQ_MOVE_TO_WAREHOUSE			0x1608
	{
		u8	bag_type;
		u64 	item_seq;				/// 아이템 시퀀스
	}

	SC_REQ_MOVE_TO_WAREHOUSE			0x1609
	{
		s16	ack;						/// 1:완료 ,  -1 실패, (창고에 공간이 없을 때)
	}*/

	PREPARE_PACKET;

	char bagType		= 0;
	uint64_t itemSeq	= 0;

	*pPacket >> bagType >> itemSeq;

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(bagType));
	if (NULL == pBag)
	{
		DEBUGLOG("CS_REQ_MOVE_TO_WAREHOUSE : pBag is NULL! bagtype=%d", bagType);
		return;
	}

	NLogic::Item * pItem = pBag->GetItem(itemSeq);
	if (NULL == pItem)
	{
		DEBUGLOG("CS_REQ_MOVE_TO_WAREHOUSE : pItem is NULL! itemseq=%lld", itemSeq);
		return;
	}

	if (true == pItem->IsEquip())
	{
		DEBUGLOG("CS_REQ_MOVE_TO_WAREHOUSE : true == pItem->IsEquip() itemseq=%lld", itemSeq);

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_REQ_MOVE_TO_WAREHOUSE);
		pUser->Send(pAck);

		return;
	}

	NLogic::Bag * pWareHouseBag = pUser->GetBag(BAG_TYPE_WAREHOUSE);
	if (NULL == pWareHouseBag)
	{
		DEBUGLOG("CS_REQ_MOVE_TO_WAREHOUSE : pWareHouseBag is NULL!");
		return;
	}

	// 가방이 꽉찬 경우
	if (true == pWareHouseBag->IsBagFull())
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_INVEN_BAG_FULL);
		pAck->MakePacket(CMD_SC_REQ_MOVE_TO_WAREHOUSE);
		pUser->Send(pAck);
		return;
	}

	// 가방 타입 변경
	pItem->ChangeBagTypeWareHouse();

	// 창고에 아이템 삽입
	pWareHouseBag->AddItem(pItem);

	// 인벤에서 아이템 제거
	pBag->PopItem(itemSeq);
	pItem->UpdateDatabaseItemInfo();

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_REQ_MOVE_TO_WAREHOUSE);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
